<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\common\Auth;
use plugin\admin\app\common\Util;
use support\Request;
use support\Response;
use plugin\admin\app\model\Work;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 作品列表 
 */
class WorkController extends Crud
{
    
    /**
     * @var Work
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new Work;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('work/index');
    }

    public function wait(): Response
    {
        return view('work/wait');
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('work/insert');
    }

    protected function updateInput(Request $request): array
    {
        $primary_key = $this->model->getKeyName();
        $id = $request->post($primary_key);
        $data = $this->inputFilter($request->post());
        if (!Auth::isSupperAdmin() && $this->dataLimit && !empty($data[$this->dataLimitField])) {
            $admin_id = $data[$this->dataLimitField];
            if (!in_array($admin_id, Auth::getScopeAdminIds(true))) {
                throw new BusinessException('无数据权限');
            }
        }
        $password_filed = 'password';
        if (isset($data[$password_filed])) {
            // 密码为空，则不更新密码
            if ($data[$password_filed] === '') {
                unset($data[$password_filed]);
            } else {
                $data[$password_filed] = Util::passwordHash($data[$password_filed]);
            }
        }

        $work = $this->model->find($id);
        if (isset($data['status'])) {
            if (!$work['audit_time']) {
                if ($data['status'] == 1) {
                    $work->audit_time = date('Y-m-d H:i:s');
                    $work->is_public = 1;
                    $work->save();
                } else {
                    $work->is_public = 0;
                    $work->save();
                }

            } else {
                if ($data['status'] == 1) {
                    $work->audit_time = date('Y-m-d H:i:s');
                    $work->save();
                } else {
                    $work->is_public = 0;
                    $work->save();
                }
            }
        }


        if (empty($data['price'])) {
            throw new BusinessException('价格不能为空', 2);
        }

        unset($data[$primary_key]);
        return [$id, $data];
    }

    public function setStatus(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $primary_key = $this->model->getKeyName();
            $id = $request->post($primary_key);
            $data = $this->inputFilter($request->post());
            if (!Auth::isSupperAdmin() && $this->dataLimit && !empty($data[$this->dataLimitField])) {
                $admin_id = $data[$this->dataLimitField];
                if (!in_array($admin_id, Auth::getScopeAdminIds(true))) {
                    throw new BusinessException('无数据权限');
                }
            }
            $password_filed = 'password';
            if (isset($data[$password_filed])) {
                // 密码为空，则不更新密码
                if ($data[$password_filed] === '') {
                    unset($data[$password_filed]);
                } else {
                    $data[$password_filed] = Util::passwordHash($data[$password_filed]);
                }
            }


            unset($data[$primary_key]);
            $this->doUpdate($id, $data);
            return $this->json(0);
        }
        return view('work/update');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('work/update');
    }

    protected function formatNormal($items, $total): Response
    {
        foreach ($items as $key=>$item) {
            $items[$key]['status_text'] = Work::STATUS_TEXT[$item['status']];

            $images = json_decode($item->images, true);
            if ($images) {
                foreach ($images as $k => $v) {
                    $images[$k] = build_oss_url($v);
                }

                $items[$key]['images'] = json_encode($images, JSON_UNESCAPED_UNICODE);
            }

            $items[$key]['thumb'] = build_oss_url($item->thumb);
        }

        return json(['code' => 0, 'msg' => 'ok', 'count' => $total, 'data' => $items]);
    }
}
